const { connect } = dva;
const { Button, Calendar, Tag, Icon, message } = antd;
import { namespace } from '../../../models/overtime/Record.jsx'

function handleSubmit(dispatch, selectDate) {
    if(!selectDate || selectDate.size === 0) {
        message.error('请选择加班日期提交');
        return;
    }
    const array = [];
    selectDate.forEach(v => array.push({ date: v }));
    dispatch(({ type: `${namespace}/save`, payload: { url: 'batchSave', values: array }}));
}

class SysModuleForm extends React.Component {
    render() {
        const { edit: { selectDate }, loading, dispatch } = this.props, month = moment().subtract(1, "months");
        return (
            <div style={{padding: 40}} className="overtime-calendar">
                <div className='search-toolbar'>
                    <Icon type='calendar' /><span style={{ paddingLeft: 2 }}>选择加班日期</span>
                    <Button type='primary' disabled={loading} style={{ marginLeft: 8 }} onClick={handleSubmit.bind(this, dispatch, selectDate)}>提交</Button>
                </div>
                <Calendar
                    defaultValue={ month }
                    dateCellRender={ date => selectDate && selectDate.has(date.format('YYYY-MM-DD')) ? <Tag color="#108ee9">加班</Tag> : undefined }
                    onSelect={ date => dispatch({ type: `${namespace}/addDate`, payload: { date } }) }
                    disabledDate={ current => {
                        return current && (current.isBefore(month.startOf('month')) || current.isAfter(month.endOf('month')));
                    } }
                />
            </div>
        );
    }
}

module.exports = connect(state => {
    return { ...state[namespace], loading: state.loading.models[namespace]  }
})(SysModuleForm);